/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionnote.action;

import fr.p10.miage.gestionnote.form.AjouterControleActionForm;
import fr.p10.miage.gestionnote.model.ControleValidateur;
import fr.p10.miage.gestionnote.model.NotesValidateur;
import fr.p10.miage.gestionnote.modelservice.ControleServices;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionRedirect;

/**
 *
 * @author charleshenzel
 */
public class ECModifierControleAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "modifier-controle-success";
    private static final String ERROR = "modifier-controle-error";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
            AjouterControleActionForm ajouterControleForm = (AjouterControleActionForm) form;

            Map<String, String> HashMap = ajouterControleForm.getHashMap();

            if(HashMap !=null)
            {
                    String controleLabel = ajouterControleForm.getLabel();
                    float controleCoeff = ajouterControleForm.getCoefficient();
                    int controleType = ajouterControleForm.getType();
                    int controleEchelle = ajouterControleForm.getEchelle();
                    
                    ActionMessages errors = new ActionMessages();
                    ControleValidateur controleValidateur= new ControleValidateur(controleLabel,controleCoeff,/*controleType,*/controleEchelle);
                    boolean valide = controleValidateur.validate(errors);
                    //System.out.println(controleCoeff);
                    
                    if(!valide)
                    {
                        saveErrors(request.getSession(), errors);
                        ActionRedirect redirect = new ActionRedirect(mapping.findForward(ERROR));
                        redirect.addParameter("idec", ajouterControleForm.getIdec());
                        redirect.addParameter("idcontrole", ajouterControleForm.getIdcontrole());
                        redirect.addParameter("idp", ajouterControleForm.getIdp());
                        return redirect;
                    }
                    
                    ControleServices.modifierControle(controleLabel,
                                                     controleCoeff,
                                                     ajouterControleForm.getIdec(),
                                                     controleType,
                                                     controleEchelle,
                                                     ajouterControleForm.getIdp(),
                                                     ajouterControleForm.getIdcontrole()
                                                     );
                    
                    Set listKeys = HashMap.keySet();
                    Iterator iterateur = listKeys.iterator();
                    String[] splitArray = null;
                    
                    NotesValidateur notesValidateur= new NotesValidateur(HashMap,controleEchelle);
                    valide = notesValidateur.validate(errors);
                    
                    if(!valide)
                    {
                        saveErrors(request.getSession(), errors);
                        ActionRedirect redirect = new ActionRedirect(mapping.findForward(ERROR));
                        redirect.addParameter("idec", ajouterControleForm.getIdec());
                        redirect.addParameter("idcontrole", ajouterControleForm.getIdcontrole());
                        redirect.addParameter("idp", ajouterControleForm.getIdp());
                        return redirect;
                    }
                    
                    while (iterateur.hasNext()) 
                    {
                        boolean dispense=false;
                        Object key = iterateur.next();
                        String noteDispense = HashMap.get(key).toString();
//                        System.out.println(noteDispense);
//                        System.out.println(key.toString());
                        splitArray = noteDispense.split("_");
                        
                        if(splitArray[1].equalsIgnoreCase("on")) 
                        {
                            // Si l'eleve courant est dispensé on passe automatiquement sa note à 0
                            splitArray[0]="0";
                            dispense=true;
                        }
                        if(!(String.valueOf(splitArray[0]).isEmpty()) || dispense==true)
                        {
                            // Si la note n'est pas vide ou que l'eleve courant est dispensé on ajouter la note
                            ControleServices.modifierNoteEtudiant(ajouterControleForm.getIdcontrole(), Integer.parseInt(key.toString()), Float.parseFloat(splitArray[0]), dispense);
                        }
                        
                    }
            }
            ActionMessages messages = new ActionMessages();
            messages.add("succesModifControle", new ActionMessage("succes.modif.controle",ajouterControleForm.getLabel()));
            saveMessages(request.getSession(), messages);
            
            ActionRedirect redirect = new ActionRedirect(mapping.findForward(SUCCESS));
            redirect.addParameter("idec", ajouterControleForm.getIdec());
            redirect.addParameter("idp", ajouterControleForm.getIdp());
            return redirect;
        
    }
}
